//
//  CTAboutUsViewController.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/28.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTAboutUsViewController.h"
#import "CTUserAgreementViewController.h"
#import "CTOfficialWebSiteViewController.h"

//header
#import "CTHeader.h"

//view
#import "CTAboutUsView.h"

//function
#import "CTDebugManager.h"
#import "CTAppInfo.h"
#import "CTAppUpdate.h"
#import "CTDeviceFunction.h"
#import "CTAppLanguage.h"
#import "CTAppUpdateItem.h"
#import <objc/runtime.h>
#import "MobClick.h"



@interface CTAboutUsViewController ()<CTAboutUsViewDelegate,UIAlertViewDelegate>
{
    CTAboutUsView * aboutUsView;
}
@end

@implementation CTAboutUsViewController


- (void)dealloc
{
    [aboutUsView removeFromSuperview];
    aboutUsView = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTCertificationViewController Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //统计
    [MobClick beginLogPageView:@"CTAboutUsViewController"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //统计
    [MobClick endLogPageView:@"CTAboutUsViewController"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    aboutUsView = [[CTAboutUsView alloc] init];
    aboutUsView.delegate = self;
    aboutUsView.versionStr = [CTAppInfo projectVersion];
    [self.view addSubview:aboutUsView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - about view delegate -

- (void) aboutUsViewBackBtnAction:(CTAboutUsView *) view
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void) aboutUsViewJoinBtnAction:(CTAboutUsView *)view
{
    
}
- (void) aboutUsViewVersionBtnAction:(CTAboutUsView *)view
{
    [self checkNewVersion];
    
}
- (void) aboutUsViewaGreementBtnAction:(CTAboutUsView *)view
{
    CTUserAgreementViewController * userAgreementVC = [[CTUserAgreementViewController alloc] init];
    userAgreementVC.showType = CTUserAgreementViewControllerShowTypePush;
    [self.navigationController pushViewController:userAgreementVC animated:YES];

}
- (void) aboutUsViewOfficialBtnAction:(CTAboutUsView *)view
{
    CTOfficialWebSiteViewController *officialVC = [[CTOfficialWebSiteViewController alloc]init];
    [self.navigationController pushViewController:officialVC animated:YES];
}






#pragma mark - 新版本更新 -
-(void)checkNewVersion
{
    __block NSString * url;
    __block BOOL isForce;
    
    
    NSString * alertTitle = [CTAppLanguage localizedStringForKey:@"alert_title" withValue:nil withTable:@"Language"];
    NSString * alertConfirmBtn = [CTAppLanguage localizedStringForKey:@"alert_confirmBtn" withValue:nil withTable:@"Language"];
    NSString * alertCancelBtn = [CTAppLanguage localizedStringForKey:@"alert_cancleBtn" withValue:nil withTable:@"Language"];
    
    [CTAppUpdate checkAppUpdateWithForceUpdateHandler:^(CTAppUpdateItem *versionInfoModel) {
        [CTDebugManager showDebugLogInfo:@"强制更新"];
        
        url = versionInfoModel.url;
        isForce = YES;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView * alertView= [[UIAlertView alloc] initWithTitle:alertTitle message:versionInfoModel.describe delegate:self cancelButtonTitle:alertCancelBtn otherButtonTitles:alertConfirmBtn, nil];
            [alertView show];
        });
        
    } CommonUpdateHandler:^(CTAppUpdateItem *versionInfoModel) {
        [CTDebugManager showDebugLogInfo:@"普通更新"];
        
        url = versionInfoModel.url;
        isForce = NO;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView * alertView= [[UIAlertView alloc] initWithTitle:alertTitle message:versionInfoModel.describe delegate:self cancelButtonTitle:alertCancelBtn otherButtonTitles:alertConfirmBtn, nil];
            [alertView show];
        });
        
    } NoneUpdateHandler:^{
        [CTDebugManager showDebugLogInfo:@"没有版本更新，什么也不做"];
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"当前为最新版本" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alertView show];
        });
        
    }];
    
    
    void (^updateComposeBlock)(UIAlertView *,NSInteger) = ^(UIAlertView * alertView,NSInteger index){
//        if (isForce == YES) {
//            if (index == 0)[CTDeviceFunction jumpToAppStore:url];
//        }else{
//            if (index == 1)[CTDeviceFunction jumpToAppStore:url];
//        }
        
        if (index == 1)[CTDeviceFunction jumpToAppStore:url];
    };
    
    void (^updateFinishBlock)() = ^(){
       // if (isForce == YES)[self checkNewVersion];
    };
    
    objc_setAssociatedObject(self, &updateComposeKey,updateComposeBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &updateFinishKey,updateFinishBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

//delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    void (^blcok)(UIAlertView *,NSInteger) = objc_getAssociatedObject(self, &updateComposeKey);
    blcok(alertView,buttonIndex);
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    void (^blcok)() = objc_getAssociatedObject(self, &updateFinishKey);
    objc_removeAssociatedObjects(self);
    blcok();
}

@end
